<?php

// @formatter:off
/**
 * @file comic.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: comic.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/frontview/actions/content/comic.php $
 */
// @formatter:on

class ActionControllerContentComic extends WCActionController {
	//
	// Magic methods.
	public function __construct() {
		parent::__construct();
		//
		// Cache works for grid view only.
		if(isset($_REQUEST["astable"])) {
			$this->_selector["extra"] = "A";
		} else {
			$index = isset($_REQUEST["image"]) ? $_REQUEST["image"] : 0;
			$this->_selector["extra"] = "I:{$index}";
		}
	}
	//
	// Public methods.
	public function check() {
		$check = parent::check();

		if($check == WC_HTTPERROR_OK) {
			global $wcProfile;
			global $wcComic;

			if(!$wcProfile->allowedTo(WC_PERM_COMIC_VIEW)) {
				global $errno;
				global $error;

				$check = WC_HTTPERROR_FORBIDDEN;
				$errno = $check;
				$error = "You're not allowed to open comics";
			}
			if($check == WC_HTTPERROR_OK && (!$wcComic || !$wcComic->ok())) {
				global $errno;
				global $error;

				$check = WC_HTTPERROR_BAD_REQUEST;
				$errno = $check;
				$error = "Invalid comic";
			}
		}

		return $check;
	}
	//
	// Protected methods.
	protected function subRun() {
		$ok = true;

		//
		// Loading global pointers.
		global $wcProfile;
		global $wcSkinConfig;
		global $wcComic;
		global $wcAction;
		global $wcCollection;

		$wcComic->expand();
		//
		// Setting section title.
		$this->_manager->PATH = "{$wcComic->directory()->name}/{$wcComic->name}";

		if(isset($_REQUEST["astable"])) {
			$this->_manager->astable = true;

			$page = isset($_REQUEST["page"]) ? $_REQUEST["page"] : 1;
			$minImgInPage = $wcSkinConfig->max_per_page->comic_astable * ($page - 1);
			$maxImgInPage = $minImgInPage + $wcSkinConfig->max_per_page->comic_astable;

			$this->_manager->PATH .= " (page {$page})";

			$paramsNoImg = requestParams(true, $wcAction, array(
				"image",
				"astable"
			));

			$images = array();
			$imgCount = 0;
			foreach($wcComic->images() as $img) {
				if($imgCount >= $minImgInPage && $imgCount < $maxImgInPage) {
					$aux = array(
						"title" => basename($img->name()),
						"link" => "{$paramsNoImg}&image={$imgCount}",
						"img" => $img->preview()
					);
					$images[] = $aux;
				}
				$imgCount++;
			}

			$this->_manager->images = $images;

			WCFVPager::GetAssignsIntoManager(ceil($imgCount / $wcSkinConfig->max_per_page->comic_astable));
		} else {
			$paramsNoImg = requestParams(true, $wcAction, array("image", "page"));
			$index = isset($_REQUEST["image"]) ? $_REQUEST["image"] : 0;
			$files = array_values($wcComic->images());
			$imgCount = count($files);

			$this->_manager->PATH .= " (".($index + 1)."/{$imgCount})";

			$first = 0;
			$previous5 = $index - 5;
			$previous = $index - 1;
			$current = $index;
			$next = $index + 1;
			$next5 = $index + 5;
			$last = $imgCount - 1;

			if($previous5 < $first) {
				$previous5 = $last + $previous5;
				if($previous5 < $first) {
					$previous5 = $first;
				}
			}
			if($previous < $first) {
				$previous = $last;
			}
			if($next > $last) {
				$next = $first;
			}
			if($next5 > $last) {
				$next5 = $first + ($next5 - $last);
				if($next5 > $last) {
					$next5 = $last;
				}
			}

			$this->_manager->imgfirst = $files[$first]->preview();
			$this->_manager->imgprev5 = $files[$previous5]->preview();
			$this->_manager->imgprev = $files[$previous]->preview();
			$this->_manager->imgcurrent = $files[$current]->preview();
			$this->_manager->imgnext = $files[$next]->preview();
			$this->_manager->imgnext5 = $files[$next5]->preview();
			$this->_manager->imglast = $files[$last]->preview();

			$this->_manager->lnkfirst = "{$paramsNoImg}&image={$first}";
			$this->_manager->lnkprev5 = "{$paramsNoImg}&image={$previous5}";
			$this->_manager->lnkprev = "{$paramsNoImg}&image={$previous}";

			$this->_manager->linktable = "{$paramsNoImg}&astable&page=".ceil(($index + 1) / $wcSkinConfig->max_per_page->comic_astable);

			$this->_manager->lnknext = "{$paramsNoImg}&image={$next}";
			$this->_manager->lnknext5 = "{$paramsNoImg}&image={$next5}";
			$this->_manager->lnklast = "{$paramsNoImg}&image={$last}";

			$this->_manager->imgmain = turlencode($files[$current]->publicUri());
			$this->_manager->info_img_name = basename($files[$current]->name());
			$this->_manager->info_number = $index + 1;
			$this->_manager->info_number_of = $imgCount;
		}
		//
		// Setting gallery info
		// @{
		if($wcProfile->allowedTo(WC_PERM_COMIC_RENAME)) {
			$this->_manager->info_name_editable = true;
		}
		if($wcProfile->allowedTo(WC_PERM_COMIC_SET_TAG)) {
			$this->_manager->info_tags_editable = true;
		}
		if($wcProfile->allowedTo(WC_PERM_COMIC_SET_COLLECTION)) {
			$this->_manager->info_coll_editable = true;
		}
		$this->_manager->info_directory_name = $wcComic->directory()->name();
		if($wcProfile->granted(WC_PERM_DIRECTORY_VIEW)) {
			$this->_manager->info_directory_link = true;
		}
		if($wcCollection) {
			$this->_manager->info_collections = $wcCollection->collectionPath();
		}

		WCViewInfoFactory::LoadInfo($wcComic, true);
		WCViewInfoFactory::LoadInterfacesInfo($wcComic, true);
		// @}

		$this->_manager->info_readrate = $wcProfile->allowedTo(WC_PERM_COMIC_SET_RATE, WC_PERM_COMIC_SET_READ);

		return $ok;
	}
}
